﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMSGSC.BLL;
using PMSGSC.DAL;
using DevExpress.XtraTab;

namespace PMSGSC.App
{
    public partial class frmHistoryDetail : DevExpress.XtraEditors.XtraForm
    {
        public frmHistoryDetail(List<History> dataSource)
        {
            InitializeComponent();
            historyBindingSource.DataSource = dataSource;
        }

        private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {

        }

        private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void gridView2_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void gridView3_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void gridView4_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        History history = new History();
        private void historyBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            history = historyBindingSource.Current as History;
            if (history != null)
            {
                richEditControl1.Text = history.WorkOrder.HistoryDetail;
                richEditControl2.Text = history.WorkOrder.OverdueReason;
                LoadDataIntoTabPage();
            }
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridView2_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridView3_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridView4_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void LoadDataIntoTabPage()
        {
            XtraTabPage tabpage = xtraTabControl1.SelectedTabPage;
            if (history != null)
            {
                switch (tabpage.Name)
                {
                    case "StockUse":
                        invoiceDetailBindingSource.DataSource = InvoiceDetail_BLL.GetDataByWorkOrderID(history.WorkOrder_ID.Value);
                        break;
                    case "ResourceUse":
                        resourceUseBindingSource.DataSource = ResourceUse_BLL.GetDataByWorkOrderID(history.WorkOrder_ID.Value, false);
                        break;
                    case "IncludeJob":
                        if (history.WorkOrder.Round_ID != null)
                            roundJobBindingSource.DataSource = RoundJob_BLL.GetDataByRoundID(history.WorkOrder.Round_ID.Value, false);
                        else
                            tabpage.PageVisible = true;


                        break;
                }
            }
            else
            {
                switch (tabpage.Name)
                {
                    case "StockUse":
                        invoiceDetailBindingSource.DataSource = null;
                        break;
                    case "ResourceUse":
                        resourceUseBindingSource.DataSource = null;
                        break;
                    case "IncludeJob":
                        if (history.WorkOrder.Round_ID != null)
                            roundJobBindingSource.DataSource = null;
                        else
                            tabpage.PageVisible = true;
                        break;
                }

            }
        }

        private void xtraTabControl1_SelectedPageChanged(object sender, TabPageChangedEventArgs e)
        {
            LoadDataIntoTabPage();
        }
    }
}